#include "Screen.h"

#include <AssertUtils.h>

/* Screen():
 *	    Constructor.
 */
Screen::Screen()
{
}

/* loadResources():
 *		Loads the resources for the screen.
 */
void Screen::loadResources()
{
	onLoadResources();
}

/* drawButtons():
 *		Draws the buttons added to this screen.
 */
void Screen::drawButtons(sf::RenderWindow* win)
{
	for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
	{
		(*it)->draw(win);
	}
}

/* addButton():
 *		Adds a button to this screen.
 */
bool Screen::addButton(Button* button)
{
	for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
	{
		if((*it) == button)
		{
			Utils::debugAssert(false, "Attempting to add the same button to a screen twice!");
			return false;
		}
	}

	m_buttons.push_back(button);
	return true;
}

/* keyPressed():
 *	    Called when a key is pressed and the screen is foremost.
 */
bool Screen::keyPressed(sf::Keyboard::Key key)
{
//	Uncomment once key-presses are a thing for buttons
//	for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
//	{
//		if ((*it)->keyPressed(key))
//		{
//			return true;
//		}
//	}

	return onKeyPressed(key);
}

/* mouseUp():
 *	    Called when a mouse button is released within the screen.
 */
bool Screen::mouseUp(sf::Mouse::Button button, sf::Vector2i mousePosition)
{
	for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
	{
		if ((*it)->mouseUp(mousePosition))
		{
			return true;
		}
	}

	return onMouseUp(button, mousePosition);
}

/* mouseDown():
 *	    Called when a mouse button is pressed within the screen.
 */
bool Screen::mouseDown(sf::Mouse::Button button, sf::Vector2i mousePosition)
{
	for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it)
	{
		if ((*it)->mouseDown(mousePosition))
		{
			return true;
		}
	}

	return onMouseDown(button, mousePosition);
}

/* mouseMoved():
 *		Called when the mouse is moved by the user.
 */
bool Screen::mouseMoved(sf::Vector2i oldPosition, sf::Vector2i newPosition)
{
	return onMouseMoved(oldPosition, newPosition);
}

/* ~Screen():
 *	    Destructor.
 */
Screen::~Screen()
{
	// clean up any buttons used
	while (!m_buttons.empty())
	{
		delete m_buttons.front();
		m_buttons.pop_front();
	}
}
